<template>
  <a-row>
    <a-col :span="5">
      <div class="org-box">
        <div class="org-box-title">
          <span>机构名称</span>
          <span class="upload" @click="addVisiable = true"><icon-plus /></span>
        </div>
        <div class="org-box-search">
          <a-input-search v-model="searchKey" @change="getOrgList" @search="getOrgList"></a-input-search>
        </div>
        <div class="org-box-tree">
          <!-- <a-tree
                       :data="orgTreeData"
                       :fieldNames="fieldNames"
                       @select="(orgCheck as any)"
                   >
                   <template #switcher-icon>
                     <icon-down />
                   </template>
                   <template #title="node">
                       <div class="title">
                           <div class="title-text">
                               <img src="@/assets/images/file_icon.png" class="icon"/>
                               <span>{{node.org_name}}1</span>
                           </div>
                           <a-space size="large">
                         <a-dropdown-button @select="handleSelect">
                           <template #content>
                             <a-doption>
                               <span>编辑<span class="sel-text"></span></span>
                             </a-doption>
                             <a-doption>
                               <span>删除<span class="sel-text"></span></span>
                             </a-doption>
                           </template>
                         </a-dropdown-button>
                       </a-space>
                       </div>
                   </template>
               </a-tree> -->
          <div class="tree-wrap">
            <div class="org-box-tree-item" v-for="(item,index) in orgList" :key="`list_${index}`" @click="handleOrgInfo(item)">
            <div class="title">
              <icon-folder />
              <span>{{item.org_name}}</span>
            </div>
            <a-space size="large">
              <a-dropdown-button>
                <template #content>
                  <a-doption
                    v-for="(elem, i) in dropItem"
                    :key="`drops_${i}`"
                    @click="handleSelect(item, elem)"
                  >
                    <span>{{ elem }}</span>
                  </a-doption>
                </template>
              </a-dropdown-button>
            </a-space>
          </div>
          </div>
        </div>
      </div>
    </a-col>
    <a-col :span="19">
      <OrgInfoview :orgId="orgId" type="out"></OrgInfoview>
    </a-col>
  </a-row>
  <AddOrgview :orgId="orgId" :visiable="addVisiable" :isEdit="isEdit" :type="type"  @onClose="orgDialogClose"></AddOrgview>
  <TipsView :orgId="orgId" :visiable="delVisiable" :orgName="orgName" :type="typeVal" :tipsText="tipsText" @onClose="orgDialogClose"></TipsView>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue';
import OrgInfoview from './OrgInfoview.vue';
import AddOrgview from './AddOrgview.vue';
import TipsView from './TipsView.vue';
import { out_org_list } from '@/api/orgManagement/orgManagement';
import * as T from '@/api/orgManagement/orgManagement_type';
import { Message } from '@arco-design/web-vue';
const props = defineProps({
  type: String
})
const searchKey = ref('');
const orgList = ref([]);
const dropItem = ['编辑', '删除'];
const orgId = ref(0);
const orgName = ref('');
const addVisiable = ref(false);
const delVisiable = ref(false);
const isEdit = ref(false);
const typeVal = ref('del')
const tipsText = ref({
  title: '',
  tips: ''
})
/**
 *
 */
const handleSelect = (node: any, name: string) => {
  orgId.value = node.code
  orgName.value = node.org_name
  console.log(orgId)
  switch(name) {
    case '新增组织':
      addVisiable.value = true
      break;
    case '编辑':
      isEdit.value = true
      addVisiable.value = true
      break;
    case '删除':
      typeVal.value = 'del'
      tipsText.value = {
        title: '是否确认删除该外部机构',
        tips: '删除后该外部机构无法恢复'
      }
      delVisiable.value = true
      break;
    default:
  }
};
/**
 * 获取组织外部机构列表
 */
const getOrgList = async () => {
  const params = {
    page: 1,
    page_size: 99,
    only_outer: 1,
    org_name: searchKey.value
  }
  let { code, data, message } = await out_org_list(params);
  console.log(data);
  if (code === 200) {
    orgList.value = data
  } else {
    Message.error(message)
  }
};
/**
 * 编辑组织弹框事件
 */
 const orgDialogClose = (e:any) => {
  addVisiable.value = false
  delVisiable.value = false
  typeVal.value = ''
  isEdit.value = false
  getOrgList()
}
/**
 * 点击机构列表事件
 */
const handleOrgInfo = (item:T.CreateOrgItem) => {
  orgId.value = item.code
}
onMounted(() => {
  getOrgList();
});
</script>
<style lang="less" scoped>
:deep(.arco-tree-node-selected) {
  color: #465a80;
  background: #e5effe;
}
:deep(.arco-tree-node-title) {
  width: 100%;
  flex: 1;
}
:deep(.arco-tree-node-title-text) {
  width: 100%;
}
:deep(.arco-btn-size-medium.arco-btn-only-icon) {
  padding-top: 2px;
}
:deep(.arco-btn-secondary[type='button']) {
  background: transparent;
  border-right: none;
}
:deep(.arco-btn-icon) {
  transform: rotate(90deg);
}
:deep(.arco-icon) {
  color: #A3B0CC;
}
.org-box {
  height: calc(100vh - 152px);
  border-right: 1px solid #ebf0fa;
  padding-right: 16px;
  &-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 16px;
    font-weight: 700;
    color: #394d73;
    line-height: 24px;
    padding-top: 16px;
    .upload {
      display: inline-block;
      width: 50px;
      height: 40px;
      line-height: 40px;
      text-align: right;
      cursor: pointer;
    }
  }
  &-search {
    margin-top: 20px;
  }
  &-tree {
    width: 100%;
    height: calc(100vh - 342px);
    margin-top: 20px;
    .tree-wrap {
      height: 100%;
      overflow-y: scroll;
    }
    .org-box-tree-item {
      display: flex;
      align-items: center;
      justify-content: space-between;
      width: 100%;
      cursor: pointer;
      &:hover {
        background: #F7F9FC;
      }
      .title {
        display: flex;
        align-items: center;
        img {
          width: 16px;
          height: 16px;
          margin-right: 6px;
        }
      }
      :deep(.arco-icon) {
        font-size: 16px;
        color: #465A80;
        margin-right: 5px;
      }
    }
    //    .title {
    //        display: flex;
    //        align-items: center;
    //        justify-content: space-between;
    //        width: 100%;
    //        &-text {
    //            display: flex;
    //            align-items: center;
    //        }
    //        .icon {
    //            width: 16px;
    //            height: 16px;
    //            margin-right: 6px;
    //        }
    //    }
  }
}
</style>
